** adjust file path
global dir "\data\"
cd "$dir"


**** Summary Statistics ****
* polling-station level
use election_sample.dta, clear
sum congress_win upa_win bjp_win congress_vote_share upa_vote_share bjp_vote_share if state_rank_norm_1!=.

* constituency-level
use election_sample_const.dta, clear
sum congress_win upa_win bjp_win congress_vote_share upa_vote_share bjp_vote_share voter_turnout

**** Baseline balance test economic variables ****
use economic_baseline_male.dta, clear
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
balancetable dummy pub_empl_ext priv_empl_ext fam_empl_ext log_daily_wage_priv log_pc_exp land education cons_exp hh_agric_labor hh_agric_selfempl unempl7 poor using "balancetable61_male.tex", vce(cluster state_dist) replace ctitles("Phase 1" "Phase 2")

use economic_baseline_female.dta, clear
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
balancetable dummy pub_empl_ext priv_empl_ext fam_empl_ext log_daily_wage_priv log_pc_exp land education cons_exp hh_agric_labor hh_agric_selfempl unempl7 poor using "balancetable61_female.tex", vce(cluster state_dist) replace ctitles("Phase 1" "Phase 2")

**** Baseline balance test political variables ****
use election_sample_const.dta, clear
preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
balancetable dummy congress_win_2004 upa_win_2004 bjp_win_2004 congress_vote_share_2004 upa_vote_share_2004 bjp_vote_share_2004 leader2004_age leader2004_namelength leader2004_sc using "balancetable2004_p1p2_test.tex", vce(cluster state_dist) replace ctitles("Phase 1" "Phase 2")
restore

**** Main results ****
use election_sample.dta, clear
preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
collapse(mean) state_dist congress_win upa_win bjp_win congress_vote_share upa_vote_share bjp_vote_share state_rank_norm_1 state_rank_norm_2 nrega_phase_1 nrega_phase_2 nrega_pred_1 nrega_pred_2, by(state_code pc_code)
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

reg upa_win nrega_pred_1 int_eff state_rank_norm_1, vce(cluster state_dist)

preserve
collapse(mean) int_eff state_dist congress_win upa_win bjp_win congress_vote_share upa_vote_share bjp_vote_share state_rank_norm_1 state_rank_norm_2 nrega_phase_1 nrega_phase_2 nrega_pred_1 nrega_pred_2, by(state_code pc_code)
reg upa_win nrega_pred_1 int_eff state_rank_norm_1, vce(cluster state_dist)
restore

**** Economic Outcomes ****
use economic_outcomes.dta, clear
preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf pub_empl_ext state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf migrated_365 state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf temp_mig state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf mig_for_work state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf remit_12 state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf cons_exp state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
keep if star_state==1
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf pub_empl_ext state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf migrated_365 state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf temp_mig state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf mig_for_work state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf remit_12 state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf cons_exp state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
keep if star_state==0
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf pub_empl_ext state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf migrated_365 state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf temp_mig state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf mig_for_work state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf remit_12 state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf cons_exp state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

**** Heterogeneity by Implementation quality ****
use election_sample.dta, clear
preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
keep if star_state==0
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
keep if star_state==1
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore


**** Further Impacts of 2009 Government Election results

preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf congress_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
keep if star_state==0
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf congress_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
keep if star_state==1
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf congress_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore
 
preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
keep if upa_state==0
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
keep if upa_state==1
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore


**** Phase 2 ****

preserve
drop if state_rank_norm_2<-3 | state_rank_norm_2>5
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_2, cutoff(1) fuzzy(nrega_phase_2 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
collapse(mean) state_dist congress_win upa_win bjp_win congress_vote_share upa_vote_share bjp_vote_share state_rank_norm_1 state_rank_norm_2 nrega_phase_1 nrega_phase_2 nrega_pred_1 nrega_pred_2, by(state_code pc_code)
drop if state_rank_norm_2<-3 | state_rank_norm_2>5
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_2, cutoff(1) fuzzy(nrega_phase_2 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
drop if state_rank_norm_2<-3 | state_rank_norm_2>5
keep if star_state==0
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_2, cutoff(1) fuzzy(nrega_phase_2 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
drop if state_rank_norm_2<-3 | state_rank_norm_2>5
keep if star_state==1
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_win state_rank_norm_2, cutoff(1) fuzzy(nrega_phase_2 tsls) reps(1000) wl(-3) wr(5)
restore


**** Variance of Votes for the Government ****
preserve
bys state_code pc_code: egen upa_var=sd(upa_win)
bys state_code pc_code: egen congress_var=sd(congress_win)

collapse(mean) upa_var congress_var state_dist state_rank_norm_1 nrega_phase_1, by(state_code pc_code)
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_var state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf congress_var state_rank_norm_1, cutoff(1) fuzzy(nrega_phase_1 tsls) reps(1000) wl(-3) wr(5)
restore

preserve
bys state_code pc_code: egen upa_var=sd(upa_win)
bys state_code pc_code: egen congress_var=sd(congress_win)
collapse(mean) upa_var congress_var state_dist state_rank_norm_2 nrega_phase_2, by(state_code pc_code)
drop if state_rank_norm_2<-3 | state_rank_norm_2>5
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf upa_var state_rank_norm_2, cutoff(1) fuzzy(nrega_phase_2 tsls) reps(1000) wl(-3) wr(5)
bootstrap, cluster(state_dist) idcluster(new) seed(22) reps(1000): rdrandinf congress_var state_rank_norm_2, cutoff(1) fuzzy(nrega_phase_2 tsls) reps(1000) wl(-3) wr(5)

restore

**** Within Parliamentary Constituency ****
preserve
keep if same_pc_diff_nregs_p1==1
reg upa_win nrega_pred_1 i.pc_code
reg congress_win nrega_pred_1 i.pc_code
restore

preserve
keep if same_pc_diff_nregs_p2==1
reg upa_win nrega_pred_2 i.pc_code
reg congress_win nrega_pred_2 i.pc_code
restore

**** Figures ****
use phase_nregs.dta, clear

* Distribution of Index Phase 1
twoway (scatter comp_index state_rank_norm_1 if state_code==3) (scatter comp_index state_rank_norm_1 if state_code==6) (scatter comp_index state_rank_norm_1 if state_code==8) (scatter comp_index state_rank_norm_1 if state_code==9) (scatter comp_index state_rank_norm_1 if state_code==10) (scatter comp_index state_rank_norm_1 if state_code==18) (scatter comp_index state_rank_norm_1 if state_code==19) (scatter comp_index state_rank_norm_1 if state_code==20) (scatter comp_index state_rank_norm_1 if state_code==21) (scatter comp_index state_rank_norm_1 if state_code==22) (scatter comp_index state_rank_norm_1 if state_code==23) (scatter comp_index state_rank_norm_1 if state_code==24) (scatter comp_index state_rank_norm_1 if state_code==27) (scatter comp_index state_rank_norm_1 if state_code==28) (scatter comp_index state_rank_norm_1 if state_code==29) (scatter comp_index state_rank_norm_1 if state_code==32) (scatter comp_index state_rank_norm_1 if state_code==33), xline(0) xtitle(Normalized State Rank Phase 1) ytitle(Planning Commission Index) legend(off) scheme(s2mono)
graph export distribution_index_p1.png, replace width(2000)

* Distribution of Index: Phase 2 vs Phase 3
twoway (scatter comp_index state_rank_norm_2 if state_code==3) (scatter comp_index state_rank_norm_2 if state_code==6) (scatter comp_index state_rank_norm_2 if state_code==8) (scatter comp_index state_rank_norm_2 if state_code==9) (scatter comp_index state_rank_norm_2 if state_code==10) (scatter comp_index state_rank_norm_2 if state_code==18) (scatter comp_index state_rank_norm_2 if state_code==19) (scatter comp_index state_rank_norm_2 if state_code==20) (scatter comp_index state_rank_norm_2 if state_code==21) (scatter comp_index state_rank_norm_2 if state_code==22) (scatter comp_index state_rank_norm_2 if state_code==23) (scatter comp_index state_rank_norm_2 if state_code==24) (scatter comp_index state_rank_norm_2 if state_code==27) (scatter comp_index state_rank_norm_2 if state_code==28) (scatter comp_index state_rank_norm_2 if state_code==29) (scatter comp_index state_rank_norm_2 if state_code==32) (scatter comp_index state_rank_norm_2 if state_code==33), xline(0) xtitle(Normalized State Rank Phase 2) ytitle(Planning Commission Index) legend(off) scheme(s2mono)
graph export distribution_index_p2.png, replace width(2000)

* Phase 1 first stage graph
preserve
keep if (nrega_pred_1==1 | nrega_pred_2==1)
count
egen bins1=cut(state_rank_norm_1), at(-20(1)42)
collapse(mean) nrega_phase_1 state_rank_norm_1, by(bins1)
count
twoway (qfitci nrega_phase_1 bins1 if bins1<=0, ciplot(rline)) (qfitci nrega_phase_1 bins1 if bins1>0, ciplot(rline))(scatter nrega_phase_1 bins1), ylabel(0 0.2 0.4 0.6 0.8 1)  legend(order(1 "95% Confidence Interval"  4 "Probability of receiving NREGS Phase 1"  )) xtitle(Normalized State Rank Phase 1) scheme(s2mono)
graph export first_stage_p1.png, replace
restore

* Phase 2 first stage graph
preserve
egen bins1=cut(state_rank_norm_2), at(-20(1)42)
collapse(mean) nrega_phase_2 state_rank_norm_2, by(bins1)
twoway (qfitci nrega_phase_2 bins1 if bins1<=0, ciplot(rline)) (qfitci nrega_phase_2 bins1 if bins1>0, ciplot(rline))(scatter nrega_phase_2 bins1), ylabel(0 0.2 0.4 0.6 0.8 1) legend(order(1 "95% Confidence Interval"  4 "Probability of receiving NREGS Phase 2"  )) xtitle(Normalized State Rank Phase 2) scheme(s2mono)
graph export first_stage_p2.png, replace
restore

* Minimum p-value figures
use economic_baseline_male.dta, clear
rdwinselect state_rank_norm_1 pub_empl_ext priv_empl_ext fam_empl_ext log_daily_wage log_pc_exp land education, cutoff(1) seed(22) statistic(ttest) reps(1000) level(.15) plot

rdwinselect state_rank_norm_2 pub_empl_ext priv_empl_ext fam_empl_ext log_daily_wage log_pc_exp land education, cutoff(1) seed(22) statistic(ttest) reps(1000) level(.15) plot

* RD
use election_sample.dta, clear
preserve
drop if state_rank_norm_1<-2 | state_rank_norm_1>4
binscatter upa_win state_rank_norm_1, nquantiles(7) linetype(lfit) rd(0.5) xtitle(Normalized State Rank) ytitle(UPA won) savegraph(upa_win1_ps.png) colors(gs0 gs5 gs10)
restore

preserve
collapse(mean) upa_win state_rank_norm_1, by(state_code pc_code)
drop if state_rank_norm_1<-3 | state_rank_norm_1>5
binscatter upa_win state_rank_norm_1, nquantiles(7) linetype(lfit) rd(0.5) xtitle(Normalized State Rank) ytitle(UPA won) savegraph(upa_win1_pc.png) colors(gs0 gs5 gs10)
restore